<script>
    // 创建三个Promise
    const p1 = new Promise((resolve, reject) => {
        setTimeout(() => {
            // resolve("p1 resolve")
            reject("p1 reject error")
        }, 3000)
    })

    const p2 = new Promise((resolve, reject) => {
        setTimeout(() => {
            // resolve("p2 resolve")
            reject("p2 reject error")
        }, 2000)
    })

    const p3 = new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve("p3 resolve")
        }, 5000)
    })


    Promise.race = function (promises) {
        return new Promise((resolve, reject) => {
            let len = promises.length;
            if (len === 0) return;
            for (let i = 0; i < len; i++) {
                Promise.resolve(promises[i]).then(data => {
                    resolve(data)
                    return;
                }).catch(err => {
                    reject(err);
                    return;
                })
            }
        })
    }

    // 类方法: race方法    race是比赛的意思
    // 特点: 会等到第一个Promise有结果(无论这个结果是fulfilled还是rejected)
    Promise.race([p1, p2, p3]).then(res => {
        console.log("race promise:", res)
    }).catch(err => {
        console.log("race promise err:", err)
    })
</script>